package Logic;

import javax.mail.*;

import javax.mail.internet.*;
import java.util.*;
/**
 * this class runing the main tra
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class SendMail
{
  String  //d_email = "pruhhey11@gmail.com",
  		  d_email ="obl.librarian@gmail.com",
		  d_password = "obl123456",
          d_host = "smtp.gmail.com",
          d_port  = "465",
          m_to = "obl.librarian@gmail.com",
          m_subject = "Extend Lending",
          m_text ;

  public SendMail()
  {
  		m_text= "Subscriber ID Number:  Extended His Lending";
      Properties props = new Properties();
      props.put("mail.smtp.user", d_email);
      props.put("mail.smtp.host", d_host);
      props.put("mail.smtp.port", d_port);
      props.put("mail.smtp.starttls.enable","true");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.debug", "true");
      props.put("mail.smtp.socketFactory.port", d_port);
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");

      SecurityManager security = System.getSecurityManager();

      try
      {
          Authenticator auth = new SMTPAuthenticator();
          Session session = Session.getInstance(props, auth);
          session.setDebug(true);
          MimeMessage msg = new MimeMessage(session);
          msg.setText(m_text);
          msg.setSubject(m_subject);
          msg.setFrom(new InternetAddress(d_email));
          msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
          Transport.send(msg);
      }
      catch (Exception mex)
      {
          mex.printStackTrace();
      } 
  }
  public SendMail(String id)
  {
  	m_text= "Subscriber ID Number:"+id+"Extend His Lending";
      Properties props = new Properties();
      props.put("mail.smtp.user", d_email);
      props.put("mail.smtp.host", d_host);
      props.put("mail.smtp.port", d_port);
      props.put("mail.smtp.starttls.enable","true");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.debug", "true");
      props.put("mail.smtp.socketFactory.port", d_port);
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");

      SecurityManager security = System.getSecurityManager();

      try
      {
          Authenticator auth = new SMTPAuthenticator();
          Session session = Session.getInstance(props, auth);
          session.setDebug(true);
          MimeMessage msg = new MimeMessage(session);
          msg.setText(m_text);
          msg.setSubject(m_subject);
          msg.setFrom(new InternetAddress(d_email));
          msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
          Transport.send(msg);
      }
      catch (Exception mex)
      {
          mex.printStackTrace();
      } 
  }
  public SendMail(String sendto,Date day)
  {
  	  m_text= "Please Return The Book You Orderd Until:"+day+" ";
 
      Properties props = new Properties();
      props.put("mail.smtp.user", d_email);
      props.put("mail.smtp.host", d_host);
      props.put("mail.smtp.port", d_port);
      props.put("mail.smtp.starttls.enable","true");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.debug", "true");
      props.put("mail.smtp.socketFactory.port", d_port);
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");

      SecurityManager security = System.getSecurityManager();

      try
      {
          Authenticator auth = new SMTPAuthenticator();
          Session session = Session.getInstance(props, auth);
          session.setDebug(true);
          MimeMessage msg = new MimeMessage(session);
          msg.setText(m_text);
          msg.setSubject(m_subject);
          msg.setFrom(new InternetAddress(d_email));
          msg.addRecipient(Message.RecipientType.TO, new InternetAddress(sendto));
          Transport.send(msg);
      }
      catch (Exception mex)
      {
          mex.printStackTrace();
      } 
  }
  public SendMail(String mailfrom,String mailto,String subject)
  {
  	  m_text= "The Book You Orderd Is Reserved From You For 2 Days ";
 
      Properties props = new Properties();
      props.put("mail.smtp.user", mailfrom);
      props.put("mail.smtp.host", d_host);
      props.put("mail.smtp.port", d_port);
      props.put("mail.smtp.starttls.enable","true");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.debug", "true");
      props.put("mail.smtp.socketFactory.port", d_port);
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");

      SecurityManager security = System.getSecurityManager();

      try
      {
          Authenticator auth = new SMTPAuthenticator();
          Session session = Session.getInstance(props, auth);
          session.setDebug(true);
          MimeMessage msg = new MimeMessage(session);
          msg.setText(m_text);
          msg.setSubject(subject);
          msg.setFrom(new InternetAddress(mailfrom));
          msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailto));
          Transport.send(msg);
      }
      catch (Exception mex)
      {
          mex.printStackTrace();
      } 
  }

  public static void main(String[] args)
  {
      SendMail blah = new SendMail();
  }

  private class SMTPAuthenticator extends javax.mail.Authenticator
  {
      public PasswordAuthentication getPasswordAuthentication()
      {
          return new PasswordAuthentication(d_email, d_password);
      }
  }
}
